<!-- 
1.默认情况下进入登录页面作如下操作：（/login,/)
	1）如果没有登录，根据服务器返回结果跳转cAS或者停留在本页面等待输入用户名、密码
    2）如果已经登录，
		a)如果没有生成授权菜单，则动态生成路由信息
		b)入首页(/dashboard)或者进入授权的第一个菜单
		c)如果没有任何授权信息，则进入401页面
-->
<template>
    <div class="login-container">
      <div class="loadWrapper" v-if="loading">
        <i class="el-icon-loading"></i>
      </div>
      <div v-else class='loadWrapper'>
  
          <el-result icon="warning" title="提示" subTitle="网络连接失败，请重试！" v-if="connectError">
            <template slot="extra">
              <el-button type="primary" @click="handleRefresh">重新加载</el-button>
            </template>
          </el-result>
  
          <el-result icon="loading" title="提示" subTitle="正在跳转登录页面" v-if="redirectLogin">
            
          </el-result>
  
          
          
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import {onMounted,ref} from 'vue' 
  import {useUserStore,useRouteStore} from '@/store/index.ts'
  import { useRouter } from 'vue-router';
  import http from '@/api/js/http'
  import appRouter from '@/router';

  const loading = ref(true);
  const connectError = ref(false);
  const userStore = useUserStore();
  const routeStore = useRouteStore();
  const router = useRouter();
  const redirectLogin = ref(false);

  onMounted(()=>{
    //获取用户信息
    http.get<IUserInfo>('/sys/current_user')
    .then((resp)=>{
    
      loading.value = false;
      connectError.value = false;

      //设置当前用户
      var userInfo = resp.data;
      userStore.login(userInfo);

      console.log(userInfo)
      
      //判断权限
      if(userInfo?.perms.length === 0){
        router.push('/401');
      }else{

        //根据权限生成路由
        if(userInfo?.perms){
          routeStore.generateRouteByPers(userInfo.perms);
          routeStore.accessedRoutes.forEach((theRouter)=>{
            appRouter.addRoute(theRouter)
          })
        }
        
       
        //判断是否需要跳转,在菜单生成哪里判断吧
        /*if(resp.extent && resp.extent != '/'){
          router.replace(resp.extent);
        } */
        
        //跳转道首页（包含菜单的页面，此页面自动打开第一个菜单）
        router.push('/index');
      }

    }).catch((err)=>{
      loading.value = false;
      if(err == '401'){
        redirectLogin.value = true;
      }else{
        connectError.value = true;
      }
    });

  });


  function  handleRefresh(){
      window.location.reload();
  }


  </script>
  
  <style rel="stylesheet/scss" lang="scss" scoped>
  .login-container{
    height:100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
  } 
  
  .loadWrapper{
    text-align: center;
  }
  
  </style>
  